package com.white.commonlib.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.white.commonlib.R;
import com.white.commonlib.manager.LogManager;
import com.zhy.autolayout.utils.AutoUtils;

import java.lang.reflect.Field;

/**
 * Created by ytf on 2017/4/7 007.
 * Description:
 */

public class CustomToolbar extends Toolbar
{

    public CustomToolbar(Context context)
    {
        this(context, null);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    CharSequence titleStr;

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        context = getContext();
        final TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs,
                R.styleable.Toolbar, defStyleAttr, 0);
        mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
        if (mTitleTextAppearance != 0)
        {
            setTitleTextAppearance(context, mTitleTextAppearance);
        }
        titleStr = a.getText(android.support.v7.appcompat.R.styleable.Toolbar_title);
        a.recycle();

        if(mTitleTextView != null && mTitleTextView.getParent() == this)
        {
            removeView(mTitleTextView);
            mTitleTextView = null;
        }
        createAndInit();
        addCenterView(mTitleTextView);
        setTitle(titleStr);
        AutoUtils.autoTextSize(mTitleTextView);
    }

    private void setLengthFilter()
    {
//        mTitleTextView.setSingleLine();
        mTitleTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
        mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
    }

    private TextView mTitleTextView;
    private int mTitleTextAppearance;

    @Override
    public void setTitle(CharSequence title)
    {
        if (mTitleTextView != null)
        {
            mTitleTextView.setText(title);
            setCenter("mTitleTextView");
        }
    }

    private void createAndInit()
    {
        Context context = getContext();
        mTitleTextView = new TextView(context);
//        LogManager.e("创建title");
        setLengthFilter();
        if (mTitleTextAppearance != 0)
        {
            mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
        }
    }

    private void addCenterView(View v)
    {
        final ViewGroup.LayoutParams vlp = v.getLayoutParams();
        final LayoutParams lp;
        if (vlp == null)
        {
            lp = generateDefaultLayoutParams();
        } else if (!checkLayoutParams(vlp))
        {
            lp = generateLayoutParams(vlp);
        } else
        {
            lp = (LayoutParams) vlp;
        }
        addView(v, lp);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams()
    {
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        return lp;
    }

    @Override
    public void setTitleTextAppearance(Context context, @StyleRes int resId)
    {
        mTitleTextAppearance = resId;
        if (mTitleTextView != null)
        {
            mTitleTextView.setTextAppearance(context, resId);
        }
    }

    @Override
    public void setNavigationIcon(@Nullable Drawable icon)
    {
        super.setNavigationIcon(icon);
        setGravityCenter();
    }

    public void setGravityCenter()
    {
        post(new Runnable()
        {
            @Override
            public void run()
            {
                setCenter("mNavButtonView");
                setCenter("mMenuView");
            }
        });
    }

    private void setCenter(String fieldName)
    {
        try
        {
            Field field = getClass().getSuperclass().getDeclaredField(fieldName);//反射得到父类Field
            field.setAccessible(true);
            Object obj = field.get(this);//拿到对应的Object
            if (obj == null) return;
            if (obj instanceof View)
            {
                View view = (View) obj;
                ViewGroup.LayoutParams lp = view.getLayoutParams();//拿到LayoutParams
                if (lp instanceof ActionBar.LayoutParams)
                {
                    ActionBar.LayoutParams params = (ActionBar.LayoutParams) lp;
                    params.gravity = Gravity.CENTER_VERTICAL;//设置居中
                    view.setLayoutParams(lp);
                }
            }
        } catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}
